﻿using SerialPortHelperLib;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO.Ports;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using XChip_Common;
using XChipTool.Enties;

namespace XChipTool
{
   public class XChipHelp
    {
        private static addressregisterManager aBll = new addressregisterManager();

       
        /// <summary>
        /// ae部分寄存器
        /// </summary>
        public static List<addressregister> XChipAe
        {
            get
            {
                if ( null ==_XChipAe)
                {
                    _XChipAe = aBll.GetList(c=>c.ChipType == ConfigurationManager.AppSettings["xc151_ChipType"] && (c.Classify =="ae" || c.Classify == "Common"));
                }
                return _XChipAe;
            }

            set
            {
                _XChipAe = value;
            }
        }

        public static List<addressregister> XChipAwb
        {
            get
            {
                if (null == _XChipAwb)
                {
                    _XChipAwb = aBll.GetList(c => c.ChipType == ConfigurationManager.AppSettings["xc151_ChipType"] && (c.Classify == "awb" || c.Classify == "Common"));
                }
                return _XChipAwb;
            }

            set
            {
                _XChipAwb = value;
            }
        }

        public static List<addressregister> XChipCmx
        {
            get
            {
                if (null == _XChipCmx)
                {
                    _XChipCmx = aBll.GetList(c => c.ChipType == ConfigurationManager.AppSettings["xc151_ChipType"] && (c.Classify == "cmx" || c.Classify == "Common"));
                }
                return _XChipCmx;
            }

            set
            {
                _XChipCmx = value;
            }
        }

        public static List<addressregister> XChipDetail
        {
            get
            {
                if (null == _XChipDetail)
                {
                    _XChipDetail = aBll.GetList(c => c.ChipType == ConfigurationManager.AppSettings["xc151_ChipType"] && (c.Classify == "detail" || c.Classify == "Common"));
                }
                return _XChipDetail;
            }

            set
            {
                _XChipDetail = value;
            }
        }

        public static List<addressregister> XChipDNR
        {
            get
            {
                if (null == _XChipDNR)
                {
                    _XChipDNR = aBll.GetList(c => c.ChipType == ConfigurationManager.AppSettings["xc151_ChipType"] && (c.Classify == "DNR" || c.Classify == "Common"));
                }
                return _XChipDNR;
            }

            set
            {
                _XChipDNR = value;
            }
        }

        public static List<addressregister> XChipGamma
        {
            get
            {
                if (null == _XChipGamma)
                {
                    _XChipGamma = aBll.GetList(c => c.ChipType == ConfigurationManager.AppSettings["xc151_ChipType"] && (c.Classify == "gamma" || c.Classify == "Common"));
                }
                return _XChipGamma;
            }

            set
            {
                _XChipGamma = value;
            }
        }

        public static List<addressregister> XChipSca
        {
            get
            {
                if (null == _XChipSca)
                {
                    _XChipSca = aBll.GetList(c => c.ChipType == ConfigurationManager.AppSettings["xc151_ChipType"] && (c.Classify == "sca" || c.Classify == "Common"));
                }
                return _XChipSca;
            }

            set
            {
                _XChipSca = value;
            }
        }

        public static List<addressregister> XChipSde
        {
            get
            {
                if (null == _XChipSde)
                {
                    _XChipSde = aBll.GetList(c => c.ChipType == ConfigurationManager.AppSettings["xc151_ChipType"] && (c.Classify == "sde" || c.Classify == "Common"));
                }
                return _XChipSde;
            }

            set
            {
                _XChipSde = value;
            }
        }
        public static List<addressregister> XChipScene
        {
            get
            {
                if (null == _XChipScene)
                {
                    _XChipScene = aBll.GetList(c => c.ChipType == ConfigurationManager.AppSettings["xc151_ChipType"] && (c.Classify == "Scene" || c.Classify == "Common"));
                }
                return _XChipScene;
            }

            set
            {
                _XChipScene = value;
            }
        }

        public static List<addressregister> XChipLenc
        {
            get
            {
                if (null == _XChipLenc)
                {
                    _XChipLenc = aBll.GetList(c => c.ChipType == ConfigurationManager.AppSettings["xc151_ChipType"] && (c.Classify == "LENC" || c.Classify == "Common"));
                }
                return _XChipLenc;
            }

            set
            {
                _XChipLenc = value;
            }
        }
        public static List<addressregister> XChipCRL
        {
            get
            {
                if (null == _XChipCRL)
                {
                    _XChipCRL = aBll.GetList(c => c.ChipType == ConfigurationManager.AppSettings["xc151_ChipType"] && (c.Classify == "CRL" || c.Classify == "Common"));
                }
                return _XChipCRL;
            }

            set
            {
                _XChipCRL = value;
            }
        }
        public static List<addressregister> XChipCADJ
        {
            get
            {
                if (null == _XChipCADJ)
                {
                    _XChipCADJ = aBll.GetList(c => c.ChipType == ConfigurationManager.AppSettings["xc151_ChipType"] && (c.Classify == "CADJ" || c.Classify == "Common"));
                }
                return _XChipCADJ;
            }

            set
            {
                _XChipCADJ = value;
            }
        }

        public static List<addressregister> XChipSnr
        {
            get
            {
                if (null == xChipSnr)
                {
                    xChipSnr = aBll.GetList(c => c.ChipType == ConfigurationManager.AppSettings["xc151_ChipType"] && (c.Classify == "SNR"));
                }
                return xChipSnr;
            }

            set
            {
                xChipSnr = value;
            }
        }

        public static List<addressregister> XChipCfa
        {
            get
            {
                if (null == xChipCfa)
                {
                    xChipCfa = aBll.GetList(c => c.ChipType == ConfigurationManager.AppSettings["xc151_ChipType"] && (c.Classify == "CFA"));
                }
                return xChipCfa;
            }

            set
            {
                xChipCfa = value;
            }
        }

        public static List<addressregister> XChipYnr
        {
            get
            {
                if (null == xChipYnr)
                {
                    xChipYnr = aBll.GetList(c => c.ChipType == ConfigurationManager.AppSettings["xc151_ChipType"] && (c.Classify == "YNR"));
                }
                return xChipYnr;
            }

            set
            {
                xChipYnr = value;
            }
        }

        public static List<addressregister> XChipYde
        {
            get
            {
                if (null == xChipYde)
                {
                    xChipYde = aBll.GetList(c => c.ChipType == ConfigurationManager.AppSettings["xc151_ChipType"] && (c.Classify == "YDE"));
                }
                return xChipYde;
            }

            set
            {
                xChipYde = value;
            }
        }

        public static List<addressregister> XChipCnr
        {
            get
            {
                if (null == xChipCnr)
                {
                    xChipCnr = aBll.GetList(c => c.ChipType == ConfigurationManager.AppSettings["xc151_ChipType"] && (c.Classify == "CNR"));
                }
                return xChipCnr;
            }

            set
            {
                xChipCnr = value;
            }
        }

        public static List<addressregister> XChipFcc
        {
            get
            {
                if (null == xChipFcc)
                {
                    xChipFcc = aBll.GetList(c => c.ChipType == ConfigurationManager.AppSettings["xc151_ChipType"] && (c.Classify == "FCC"));
                }
                return xChipFcc;
            }

            set
            {
                xChipFcc = value;
            }
        }

        private static List<addressregister> xChipCnr;
        private static List<addressregister> xChipYde;
        private static List<addressregister> xChipYnr;
        private static List<addressregister> xChipCfa;
        private static List<addressregister> xChipSnr;
        private static List<addressregister> xChipFcc;

        private static List<addressregister> _XChipAe;
        private static List<addressregister> _XChipAwb;
        private static List<addressregister> _XChipCmx;
        private static List<addressregister> _XChipDetail;
        private static List<addressregister> _XChipDNR;
        private static List<addressregister> _XChipGamma;
        private static List<addressregister> _XChipSca;
        private static List<addressregister> _XChipSde;
        private static List<addressregister> _XChipScene;
        private static List<addressregister> _XChipLenc;
        private static List<addressregister> _XChipCRL;
        private static List<addressregister> _XChipCADJ;

        public static string ReadParameterExportExpand(List<parameter_export_expand> tempList,string fffadd,List<parameter_export_patch> patchList, SerialPort sPortTemp) {
            StringBuilder sb = new StringBuilder();
            sb.Append($"\n");
            sb.Append($"0xfffd,0x{fffadd},\r\n");
            sb.Append($"\n");
            sb.Append($"//{tempList.First().Module}\n");
            var strNote = tempList.First().Module;
            foreach (var item in tempList)
            {
                try
                {
                    if (strNote != item.Module)
                    {
                        sb.Append($"\n//{item.Module}\n");
                        strNote = item.Module;
                    }
                    sb.Append(string.IsNullOrEmpty(item.PreNote) ? "" : "\n" + item.PreNote + "\n");
                    sb.Append(string.IsNullOrEmpty(item.addr) ? "" : item.addr);
                    #region 判断是否需要用白天的数据
                    if (!string.IsNullOrEmpty(item.ReadAddr))
                    {
                        if (patchList.Count != 0 && patchList.Select(c => c.ReadAddr).Contains(item.ReadAddr))
                        {
                            sb.Append($",0x{SerialSend.ReadRegister((Convert.ToUInt32(item.RelationAddr.Trim(), 16)), sPortTemp).ToString("x2")},");
                        }
                        else
                        {
                            sb.Append($",0x{SerialSend.ReadRegister((Convert.ToUInt32(item.ReadAddr.Trim(), 16)), sPortTemp).ToString("x2")},");
                        }
                    }
                    #endregion
                    sb.Append(string.IsNullOrEmpty(item.RearNote) ? "" : item.RearNote = item.RearNote.Replace("\\n", "\n").Replace("\\r", "\r"));
                    sb.Append("\n");
                }
                catch (Exception ex)
                {
                    return ex.Message.ToString();
                }
            }


            return sb.ToString();
        }

        public static string ReadParameterExportExpandUSB(List<parameter_export_expand> tempList, string fffadd, List<parameter_export_patch> patchList, SerialPort sPortTemp)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append($"\n");
            sb.Append($"0xfffd,0x{fffadd},\r\n");
            sb.Append($"\n");
            sb.Append($"//{tempList.First().Module}\n");
            var strNote = tempList.First().Module;
            foreach (var item in tempList)
            {
                try
                {
                    if (strNote != item.Module)
                    {
                        sb.Append($"\n//{item.Module}\n");
                        strNote = item.Module;
                    }
                    sb.Append(string.IsNullOrEmpty(item.PreNote) ? "" : "\n" + item.PreNote + "\n");
                    sb.Append(string.IsNullOrEmpty(item.addr) ? "" : item.addr);
                    #region 判断是否需要用白天的数据
                    if (!string.IsNullOrEmpty(item.ReadAddr))
                    {
                        if (patchList.Count != 0 && patchList.Select(c => c.ReadAddr).Contains(item.ReadAddr))
                        {
                            uint pVule = 0x0;
                            XChipUSB.UsbRegisterRead64(Convert.ToUInt32(item.RelationAddr.Trim(), 16), ref pVule, false);
                            sb.Append($",0x{pVule.ToString("x2")},");
                        }
                        else
                        {
                            uint pVule = 0x0;
                            XChipUSB.UsbRegisterRead64(Convert.ToUInt32(item.ReadAddr.Trim(), 16), ref pVule, false);
                            sb.Append($",0x{pVule.ToString("x2")},");
                        }
                    }
                    #endregion
                    sb.Append(string.IsNullOrEmpty(item.RearNote) ? "" : item.RearNote = item.RearNote.Replace("\\n", "\n").Replace("\\r", "\r"));
                    sb.Append("\n");
                }
                catch (Exception ex)
                {
                    return ex.Message.ToString();
                }
            }


            return sb.ToString();
        }

    }
}
